//
//  HPYEmoticonBottomView.swift
//  SwiftVVBo
//
//  Created by hepeiyong on 16/8/18.
//  Copyright © 2016年 hpyhn. All rights reserved.
//

import UIKit
// 按钮枚举
enum HPYEmoticonBottomViewType: Int {
    // 默认
    case Default = 0
    // emoji
    case Emoji = 1
    // 浪小花
    case Lxh = 2
}

//UIStackView 容器视图 ios 9.0 以上可以使用
@available(iOS 9.0, *)
class HPYEmoticonBottomView: UIStackView {
    // 当前选中的按钮
    var currentButton: UIButton?
    // 定义一个闭包01
    var closure:((type: HPYEmoticonBottomViewType)->())?

    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    // MARK: - 监听方法
    func buttonClick(button: UIButton) {
        // 如果当前选中的button和这次的相同将不再设置
        if currentButton == button {
            return
        }
        // 取消上个选中的按钮
        currentButton?.selected = false
        // 把当前的选中
        button.selected = true
        // 赋值
        currentButton = button

        // 执行闭包 03
        closure?(type: HPYEmoticonBottomViewType(rawValue: button.tag)!)
    }

    // MARK: - 设置视图
    private func setupUI(){
        // 填充方向
        axis = .Horizontal
        // 填充方式
        distribution = .FillEqually
        addChildButtons("left", title: "默认",type: .Default)
        addChildButtons("mid", title: "Emoji",type: .Emoji)
        addChildButtons("right", title: "浪小花",type: .Lxh)
        //        let defaultButton = addChildButtons("left", title: "默认")
        //        let emojiButton = addChildButtons("mid", title: "Emoji")
        //        let lxhButton = addChildButtons("right", title: "浪小花")

        //        // 添加约束
        //        defaultButton.snp_makeConstraints { (make) -> Void in
        //            make.top.leading.bottom.equalTo(self)
        //            make.width.equalTo(emojiButton)
        //        }
        //
        //        emojiButton.snp_makeConstraints { (make) -> Void in
        //            make.top.bottom.equalTo(self)
        //            make.leading.equalTo(defaultButton.snp_trailing)
        //            make.width.equalTo(lxhButton)
        //        }
        //
        //        lxhButton.snp_makeConstraints { (make) -> Void in
        //            make.top.bottom.trailing.equalTo(self)
        //            make.leading.equalTo(emojiButton.snp_trailing)
        //        }

    }
    /**
     创建按钮

     - parameter imgName: 图片名
     - parameter title:   title
     */
    func addChildButtons(imgName: String, title: String, type: HPYEmoticonBottomViewType) -> UIButton{
        let button = UIButton()
        button.tag = type.rawValue
        // 添加事件
        button.addTarget(self, action: #selector(HPYEmoticonBottomView.buttonClick(_:)), forControlEvents: UIControlEvents.TouchUpInside)
        button.setBackgroundImage(UIImage(named: "compose_emotion_table_\(imgName)_normal"), forState: UIControlState.Normal)
        button.setBackgroundImage(UIImage(named: "compose_emotion_table_\(imgName)_selected"), forState: UIControlState.Selected)
        button.setTitle(title, forState: UIControlState.Normal)
        button.setTitleColor(UIColor.whiteColor(), forState: UIControlState.Normal)
        button.setTitleColor(UIColor.darkGrayColor(), forState: UIControlState.Selected)
        button.titleLabel?.font = UIFont.systemFontOfSize(15)
        // 添加按钮
        addArrangedSubview(button)
        return button
    }
}
